function loadscript(longitude, latitude)
{
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://api.map.baidu.com/api?v=2.0&ak=RfYi5LFm8OtfKdcUiVPqTfC0ucVV3ElG&callback=init(longitude, latitude)";
	document.body.appendChild(script);
}
function init(longitude, latitude)
{
	var map = new BMap.Map("baidu");
	var point = new BMap.Point(longitude, latitude);
	var myIcon = new BMap.Icon("http://developer.baidu.com/map/jsdemo/img/fox.gif", new BMap.Size(300,157));
	var marker2 = new BMap.Marker(point);
	var top_left_control = new BMap.ScaleControl({anchor: BMAP_ANCHOR_TOP_LEFT});
	var navigationControl = new BMap.NavigationControl({
			anchor: BMAP_ANCHOR_TOP_LEFT,
			type: BMAP_NAVIGATION_CONTROL_LARGE
		  });
	var geolocationControl = new BMap.GeolocationControl();
	geolocationControl.addEventListener("locationSuccess", function(e){
		var address = '';
		address += e.addressComponent.province;
		address += e.addressComponent.city;
		address += e.addressComponent.district;
		address += e.addressComponent.street;
		address += e.addressComponent.streetNumber;
		alert("当前定位地址为：" + address);
	});
	geolocationControl.addEventListener("locationError", function(e){
		alert(e.message);
	})
	map.centerAndZoom(point, 8);
	map.addOverlay(marker2);
	marker2.setAnimation(BMAP_ANIMATION_BOUNCE);
	map.addControl(new BMap.MapTypeControl());
	map.addControl(top_left_control);
	map.addControl(navigationControl);
	map.addControl(geolocationControl);
	map.enableScrollWheelZoom(true);
	return map;
}

function history_line(data)
{
	var show_point = [];
	data_length = data.length;
	msg = JSON.parse(data[0])['msg'];
	if(data_length > 1)
	{	$.alert({
			title: '提醒!',
			content: msg,
			theme: 'material',
			animationSpeed: 300,
			autoClose: 'confirm|2000',
		});
	}
	else{
		$.alert({
			title: '提醒!',
			content: '没有历史轨迹',
			theme: 'material',
			animationSpeed: 300,
			autoClose: 'confirm|2000',
		});
	}
	for(var i=1;i<data.length;i++)
	{
		point_data = JSON.parse(data[i]); 
		show_point[i-1] = new BMap.Point(point_data['longitude'],point_data['latitude']);
	}
	var curve = new BMapLib.CurveLine(show_point, {strokeColor:"black", strokeWeight:3, strokeOpacity:0.5}); //创建弧线对象
	map = init((JSON.parse(data[1]))['longitude'], (JSON.parse(data[1]))['latitude']);
	map.addOverlay(curve);
}

function ship_location(longitude, latitude)
{
	map = init(longitude, latitude);
}

var longitude = 120.2;
var latitude = 30.3;
window.onload = loadscript(longitude, latitude);

/*
 * param: ship_id
 * success: return history_line()
 * error: alert
 */
function get_ship_location(ship_id)
{
	url = '/map/ship_id/'+ship_id;
	$.ajax({
		url: url,
		dataType: 'json',
		success: function(data){
			ship_location(data['longitude'], data['latitude']);
		},
		error:function(data){
			$.alert({
			title: '提醒!',
			content: "非常抱歉！没有找到定位相关数据",
			theme: 'material',
			animationSpeed: 300,
			autoClose: 'confirm|2000',
			});
		}
	});
}

/*
 *param: ship_id
 *get: detail table in detail.html
*/
function get_ship_detail(ship_id)
{
	get_ship_location(ship_id);
	url = '/map/ship_detail/'+ship_id;
	$.dialog({
			title: 'Belfu船舶管理系统!',
			content: "url:"+url,
			theme: 'material',
			animationSpeed: 500,
		});
}

function get_history_line(ship_id)
{
	url = '/map/history_line/'+ship_id;
	$.ajax({
		url: url,
		dataType: 'json',
		success: function(data){
			history_line(data);
		},
		error:function(data){
			$.alert({
			title: '提醒!',
			content: "非常抱歉！没有找到历史轨迹相关数据",
			theme: 'material',
			animationSpeed: 300,
			autoClose: 'confirm|2000',
			});
		}
	});
}